package apisnow;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;


public class Cleaner
{


	public static void main(String[] args) {
		System.out.println("begin to clean google.com;googleapis.com;google-analytics.com");
		String currentDir = "D:/android/docs/reference";

		Cleaner c = new Cleaner();
		c.work(currentDir);
		System.out.println("end to clean google.com;googleapis.com;google-analytics.com");	
	}

	public Cleaner(){}

	public void work(String currentDir){
		File file = new File(currentDir);
		deleteReference(file);
	}

	public void deleteReference(File file)
	{
		String[] files = file.list();
		String path = file.getPath();
		for (String one : files) {
			String filePath = path+File.separatorChar+one;
			// System.out.println(filePath);	
			File fileTemp = new File(filePath);	
			if(fileTemp.isDirectory())
			{
				deleteReference(fileTemp);
			}else{
				// delete google reference
				if(filePath.endsWith(".html"))
				{
					try
					{
						BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileTemp),"UTF-8"));

						filePath = filePath+".tmp";
						File newFile = new File(filePath);
						BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newFile),"UTF-8"));
						String line = null;
						while((line = br.readLine())!= null)
						{
							String newLine = line.replaceAll("google\\.com|googleapis\\.com|google-analytics\\.com","");
							bw.write(newLine);
							bw.newLine();
						}
						br.close();
						bw.close();
						fileTemp.delete();
						newFile.renameTo(fileTemp);
					}catch(Exception e)
					{
						e.printStackTrace();
					}
				}
			}		
		}	
	}

}

